package com.student.health.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.student.health.advice.ExceptionEnums;
import com.student.health.advice.MyException;
import com.student.health.entity.CheckInfo;
import com.student.health.entity.Foods;
import com.student.health.service.FoodsService;
import com.student.health.utils.Vo.FoodDetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author:
 * @Date: 2024-03-31 17:59
 * @Description:
 * @Version 1.0
 */
@Slf4j
@Api(tags = "体检表信息接口")
@RestController
@RequestMapping(value = "api/food")
public class FoodController {
    @Autowired
    private FoodsService foodsService;

    ///**
    // *     获取七天推荐食谱
    // */
    //@RequestMapping("/getFoodDataAnalysis/{userId}")
    //public ResponseEntity<FoodsDaySuggestDataDTO> getFoodDataAnalysis(@PathVariable("userId") Integer userId){
    //   FoodsDaySuggestDataDTO result =  foodsService.getFoodDataAnalysis(userId);
    //    if (result == null) {
    //        throw new MyException(ExceptionEnums.FOOD_ERROR);
    //    }
    //    return ResponseEntity.ok(result);
    //}

    @ApiOperation(value = "基础接口: 返回指定ID的数据")
    @GetMapping(value = "get/{id}")
    public ResponseEntity<FoodDetailVo> get(@PathVariable("id") String id) {
        FoodDetailVo res = foodsService.getFoodById(id);
        if (res != null) {
            return ResponseEntity.ok(res);
        }
        throw new MyException(ExceptionEnums.GET_ITEM);
    }

    @GetMapping("/list")
    public ResponseEntity<List<Foods>> getList(Foods foods){
        List<Foods> res =  foodsService.getList(foods);
            if (res == null) {
                throw new MyException(ExceptionEnums.FOOD_ERROR);
            }
        return ResponseEntity.ok(res);
    }

    @ApiOperation(value = "基础接口: 分页返回数据")
    @PostMapping(value = "/page")
    public ResponseEntity<Page<Foods>> page(@RequestBody Map<String,Object> param) {
        return ResponseEntity.ok(foodsService.selectPage(param));
    }

    @PostMapping(value = "add")
    public ResponseEntity<FoodDetailVo> save(@RequestBody FoodDetailVo foods) {
        if (foods.getName() != null && foods.getCalory() != null && foods.getCategory() !=null) {
            boolean save = foodsService.saveFood(foods);
            if (save) {
                return ResponseEntity.ok(foods);
            }
        }
        throw new MyException(ExceptionEnums.ADD_ERROR);
    }


    @ApiOperation(value = "基础接口: 修改数据")
    @PostMapping(value = "update")
    @RequiresPermissions("checkInfo:update")
    public ResponseEntity<FoodDetailVo> update(@RequestBody FoodDetailVo foodDetailVo) {
        boolean b = foodsService.updateFoodsById(foodDetailVo);
        if (b) {
            return ResponseEntity.ok(foodDetailVo);
        }
        throw new MyException(ExceptionEnums.UPDATE_ERROR);
    }

    @ApiOperation(value = "基础接口: 删除指定ID的数据")
    @GetMapping(value = "delete/{id}")
    @RequiresPermissions("checkInfo:delete")
    public ResponseEntity<String> delete(@PathVariable("id") String id) {
        boolean b = foodsService.removeFoodsById(id);
        if (b) {
            return ResponseEntity.ok("删除成功");
        }
        throw new MyException(ExceptionEnums.DELETE_ERROR);
    }

}
